import type { TYPE } from "./pages/types";
import { history } from "@umijs/max";

/**
 * 权限管理
 * @see https://pro.ant.design/zh-CN/docs/authority-management
 * @see https://umijs.org/docs/max/access
 */
export default function(initialState: TYPE.InitialState) {
  if (!initialState) return {
    canUse: (action: string) => true,
  };

  const { accessData } = initialState;

  return {
    canUse: (action: string) => {
      const { pathname } = history.location;
      const res = pathname.substring(1) + "/" + action;
      return accessData.indexOf(res) >= 0;
    },
  };
};
